package org.etom.impl.ui.preference;

import org.eclipse.jface.preference.IPreferenceStore;
import org.eclipse.jface.preference.PreferencePage;
import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Text;
import org.eclipse.ui.IWorkbench;
import org.eclipse.ui.IWorkbenchPreferencePage;
import org.etom.impl.ui.Activator;

/**
 * Preference page contains the settings of the plug-in. 
 */
public class SettingsPreferencePage extends PreferencePage
                                  implements IWorkbenchPreferencePage {

  private static final String UPLOAD_FILE_NAME_TEMPLATE = "moodle_preference_upload_file_name_template"; //$NON-NLS-1$
  
  private Composite composite;
  private Text uploadFileNameText;
  
  /*~~~[PreferencePage methods]~~~*/
  @Override
  protected Control createContents(Composite parent) {
    initComposite(parent);
    addControls();
    loadUploadFileNameTemplate(false);
    initPreferencePage();
    return composite;
  }
  
  @Override
  protected void performDefaults() {
    loadUploadFileNameTemplate(true);
    super.performDefaults();
  }

  @Override
  public boolean performOk() {
    save();
    return super.performOk();
  }
  
  /*~~~[PreferencePage methods]~~~*/

  @Override
  public void init(IWorkbench workbench) {
    //do nothing here
  }
  
  /*
   * The method has package-private visibility.
   */
  static String getUploadFileNameTemplate() {
    return SettingsPreferencePage.getUploadFileNameTemplate(false);
  }
  
  private static String getUploadFileNameTemplate(boolean getDefault) {
    IPreferenceStore store = Activator.getDefault().getPreferenceStore();
    String value = getDefault ? "" : store.getString(UPLOAD_FILE_NAME_TEMPLATE); //$NON-NLS-1$
    if (value.length() == 0) {
      value = store.getDefaultString(UPLOAD_FILE_NAME_TEMPLATE);
      if (value.length() == 0) {
        value = PreferenceMessages.Upload_File_Name_Assignment;
      }
    }
    return value;
  }
  
  private static void setUploadFileName(String fileName) {
    IPreferenceStore store = Activator.getDefault().getPreferenceStore();
    store.setValue(UPLOAD_FILE_NAME_TEMPLATE, fileName);
  }
  
  /*~~~[Utility methods]~~~*/
  private void save() {
    setUploadFileName(uploadFileNameText.getText());
  }
  
  private void loadUploadFileNameTemplate(boolean  setDefault) {
    uploadFileNameText.setText(
        SettingsPreferencePage.getUploadFileNameTemplate(setDefault));
  }
  
  private void initComposite(Composite parent) {
    composite = new Composite(parent, SWT.NONE);
    GridLayout layout = new GridLayout(2, false);
    composite.setLayout(layout);
  }

  private void addControls() {
    //init the label
    Label uploadFileNameLabel = new Label(composite, SWT.NONE);
    uploadFileNameLabel.setText(PreferenceMessages.Upload_File_Name_Label);
    
    //init the text field
    uploadFileNameText = new Text(composite, SWT.BORDER);
    GridData gridData = new GridData();
    gridData.horizontalAlignment = SWT.FILL;
    gridData.grabExcessHorizontalSpace = true;
    uploadFileNameText.setLayoutData(gridData);
  }
  
  private void initPreferencePage() {
    setTitle(PreferenceMessages.Upload_Preference_Page_Title);
    setDescription(PreferenceMessages.Upload_Preference_Page_Desc);
  }

}
